<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: 'InitialPage' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-button @click="addDialogVisible = true" type="primary">添加用户</el-button>
      <el-table :data="rolesList" stripe  border>
        <!-- 展开列 -->
        <el-table-column  type="expand" style="width: 60px">
          <template slot-scope="scope">
            <el-row v-for="(item1, i1) in scope.row.children" :key="item1.id" :class="['borderBottom', i1 === 0? 'borderTop': '']">
              <!-- 一级权限 -->
              <el-col :span="5" >
                <el-tag closable @close="delAuthById(scope.row, item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-arrow-right"></i>
              </el-col>
              <!-- 二级和三级权限 -->
              <el-col :span="19">
                <!-- 二级权限 -->
                <el-row v-for="(item2, i2) in item1.children" :key="item2.id" :class="i2 === 0? '': 'borderTop'">
                  <el-col :span="6">
                    <el-tag type="success" 
                closable @close="delAuthById(scope.row, item2.id)">{{item2.authName}}</el-tag>
                    <i class="el-icon-arrow-right"></i>
                  </el-col>
                  <!-- 三级权限 -->
                  <el-col :span="18"> 
                      <el-tag  type="warning" v-for="(item3, i3) in item2.children" :key="item3.id" closable @close="delAuthById(scope.row, item3.id)">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" style="width: 60px"></el-table-column>
        <el-table-column  label="角色名称" prop="roleName"></el-table-column>
        <el-table-column   label="角色描述" prop="roleDescription"></el-table-column>
        <el-table-column   label="操作">
          <template>
            <el-button type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete">删除</el-button>
            <el-button type="warning" icon="el-icon-setting" @click="assignPower">分配角色</el-button>
          </template>
        </el-table-column>
        </el-table>
    </el-card>

    <!-- 添加用户 -->
    <el-dialog
      title="提示"
      :visible.sync="addDialogVisible"
      width="35%">
      <el-form  :rules="addRules" ref="addNewUserRef">
        <el-form-item label-width="70px" label="用户名称" prop="name">
          <el-input v-model="addUser.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label-width="70px " label="用户密码" prop="password">
          <el-input v-model="addUser.password" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label-width="70px" label="联系方式" prop="tel">
          <el-input v-model="addUser.tel" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addNewUser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 分配角色权限树 -->
    <el-dialog
      title="分配权限"
      :visible.sync="assignPowerDialogVisible"
      width="35%">
      <el-tree
      :data="powersList"
      show-checkbox
      default-expand-all
      :props="treeProps"
      >
    </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="assignPowerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addNewUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 角色列表
      rolesList: [
        {
          id: '01',
          roleName: '主管',
          roleDescription: '技术负责人',
          children: [
            {
              // 一级权限
              id: '11',
              authName: '商品管理',
              children: [
                  {
                    // 二级权限
                    id: '111',
                    authName: '商品列表',
                    children: [
                      {
                        // 三级权限
                        id: '1111',
                        authName: '商品修改'
                      }
                    ]
                  },
                  {
                    id: '112',
                    authName: '参数分类',
                    children: [
                      {
                        id: '1121',
                        authName: '获取参数列表',
                      },
                      {
                        id: '1122',
                        authName: '创建商品参数'
                      },
                      {
                        id: '1123',
                        authName: '删除商品参数'
                      }
                    ]
                  },
                  {
                    id: '113',
                    authName: '商品分类',
                    children: [
                      {
                        id: '1131',
                        authName: '添加分类'
                      },
                      {
                        id: '1132',
                        authName: '删除分类'
                      },
                      {
                        id: '1133',
                        authName: '获取分类详情'
                      }
                    ]
                  }
              ]
            },
            {
              id: '12',
              authName: '订单管理',
              children: [
                {
                  id: '121',
                  authName: '订单列表',
                  children: [
                    {
                      id: '1211',
                      authName: '添加订单'
                    }
                  ]
                }
              ]
            },
            {
              id: '13',
              authName: '权限管理',
              children: [
                {
                  id: '131',
                  authName: '角色列表',
                  children: [
                    {
                      id: '1311',
                      authName: '添加角色'
                    },
                    {
                      id: '1312',
                      authName: '删除角色'
                    },
                    {
                      id: '1313',
                      authName: '角色授权'
                    },
                    {
                      id: '1314',
                      authName: '取消角色授权'
                    },
                    {
                      id: '1315',
                      authName: '获取角色列表'
                    },
                    {
                      id: '1316',
                      authName: '获取角色详情'
                    },
                    {
                      id: '1317',
                      authName: '更新角色信息'
                    },
                    {
                      id: '1318',
                      authName: '更新角色权限'
                    }
                  ]
                },
                {
                  id: '132',
                  authName: '权限列表',
                  children: [
                    {
                      id: '1321',
                      authName: '查看权限'
                    }
                  ]
                }
              ]
            }
          ]
        }
      ],
      // 权限列表
      powersList: [
        {
              // 一级权限
              id: '11',
              authName: '商品管理',
              children: [
                  {
                    // 二级权限
                    id: '111',
                    authName: '商品列表',
                    children: [
                      {
                        // 三级权限
                        id: '1111',
                        authName: '商品修改'
                      }
                    ]
                  },
                  {
                    id: '112',
                    authName: '参数分类',
                    children: [
                      {
                        id: '1121',
                        authName: '获取参数列表',
                      },
                      {
                        id: '1122',
                        authName: '创建商品参数'
                      },
                      {
                        id: '1123',
                        authName: '删除商品参数'
                      }
                    ]
                  },
                  {
                    id: '113',
                    authName: '商品分类',
                    children: [
                      {
                        id: '1131',
                        authName: '添加分类'
                      },
                      {
                        id: '1132',
                        authName: '删除分类'
                      },
                      {
                        id: '1133',
                        authName: '获取分类详情'
                      }
                    ]
                  }
              ]
        },
        {
              id: '12',
              authName: '订单管理',
              children: [
                {
                  id: '121',
                  authName: '订单列表',
                  children: [
                    {
                      id: '1211',
                      authName: '添加订单'
                    }
                  ]
                }
              ]
        },
        {
              id: '13',
              authName: '权限管理',
              children: [
                {
                  id: '131',
                  authName: '角色列表',
                  children: [
                    {
                      id: '1311',
                      authName: '添加角色'
                    },
                    {
                      id: '1312',
                      authName: '删除角色'
                    },
                    {
                      id: '1313',
                      authName: '角色授权'
                    },
                    {
                      id: '1314',
                      authName: '取消角色授权'
                    },
                    {
                      id: '1315',
                      authName: '获取角色列表'
                    },
                    {
                      id: '1316',
                      authName: '获取角色详情'
                    },
                    {
                      id: '1317',
                      authName: '更新角色信息'
                    },
                    {
                      id: '1318',
                      authName: '更新角色权限'
                    }
                  ]
                },
                {
                  id: '132',
                  authName: '权限列表',
                  children: [
                    {
                      id: '1321',
                      authName: '查看权限'
                    }
                  ]
                }
              
              ]
        }  
      ],
      // 控制添加用户对话框显示
      addDialogVisible: false,
      // 添加用户需要的属性
      addUser: {
        name: '',
        password: '',
        tel: ''
      },
      // 验证规则属性
      addRules: {
        name: [],
        password: [],
        tel:[]
      },
      // 树形数据绑定
      treeProps: {
        children: 'children',
        lable: 'authName'
      },
      // 控制分配角色权限对话框显示
      assignPowerDialogVisible: false
      
    }
  },
  created() {
    this.getRolesList()
  },
  methods: {
    // 获取角色列表
    async getRolesList() {
      try{
        const {data: res} = await this.$http.get('powers', rolesList)
        this.rolesList = res.data
        this.$message.success('获取角色列表成功')
      }
      catch(e) {
        this.$message.error('获取角色列表失败')
      }
    },
    // 添加新用户
    addNewUser() {
      // 预校验
      this.$refs.addNewUserRef.validate(async valid =>{
        if(!valid) return;
        // 发送添加请求
        try{
          const {data: res} = await this.$http.post('users', addUser)
          this.$message.success('添加新用户成功')
          this.addDialogVisible = false
        }
        catch(error) {
          this.$message.error('添加新用户失败')
          console.log(error);
        }
      })
    },
    // 发起删除请求完成权限删除
    async delAuthById(role, powerId) {
      // 弹出确认对话框提醒用户是否删除
      const confirm = await this.$confirm('此操作将删除该权限, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
        })
        .catch((error) => error);   
        // 选择取消则弹窗提示‘已取消删除’
        if(confirm !== 'confirm') {
          this.$message.info('已取消删除')
        }
        // 选择确定则发起删除请求
        const {data: res} = await this.$http.delete(`users/${role.id}/powers/${powerId}`)
        // 如果响应的状态吗不是200，则提示失败
        if(res.meta.status !== 200) {
          return this.$message.error('删除失败')
          console.log(error);
        }
        // 否则提示删除成功并将响应的data数据重新赋值给rolesList实现更新
        else {
          this.$message.success('删除成功')
          this.rolesList = res.data
        }   
    },
    // 控制分配角色弹框显示
    async assignPower() {
      this.assignPowerDialogVisible = true
      try{
        const {data: res} = await this.$http.get('powers/tree')
        this.powersList = res.data
      }
      catch(error) {
        this.$message.error('获取权限数据失败')
      }
      console.log(this.powersList);
    }
  }
}
</script>

<style lang="less" scoped>
  .el-table {
    margin-top: 15px;
  }
  .el-tag {
    margin-top: 10px;
  }
  .borderBottom {
    border-bottom: 1px solid #EBEEF5;
    display: flex;
    align-items: center;
  }
  .borderTop {
    border-top: 1px solid #EBEEF5;
  }
</style>